<?php
// +----------------------------------------------------------------------
// | Description: 客户
// +----------------------------------------------------------------------
// | Author: zl
// +----------------------------------------------------------------------

namespace app\crm\controller;

use app\admin\controller\ApiCommon;
use app\crm\model\BalanceLog;
use app\crm\model\Setting;
use app\crm\model\SettingCategory;
use app\crm\model\Service;
use app\crm\model\ReturnVist;
use app\crm\model\ReturnVistT;
use app\crm\model\Customer as CustomerModel;
use app\crm\model\CustomerLossModel;
use app\crm\model\TransferShop;
use app\crm\model\CustomerConfig as CustomerConfigModel;
use app\crm\model\CustomerGrade as CustomerGradeModel;
use think\response\Json;
use think\facade\Db;
use Kingdee\Kingdee;

class CustomerLoss extends ApiCommon
{
    protected $customerModel;
    protected $customerConfigModel;
    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @other 其他根据系统设置
     **/
    public function initialize()
    {
        $action = [
            'permission' => ['house_list','setcustomerage','setzycustomer','dsmonth','setmonth'],
            'allow' => ['users', 'info']
        ];
        event('CheckAuth',$action);
        $request = $this->request;
        $a = strtolower($request->action());

        $this->customerModel = new CustomerModel();
        $this->customerConfigModel = new CustomerConfigModel();
        if (!in_array($a, $action['permission'])) {
            parent::initialize();
        }
    }

    /**
     * 列表
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function list(CustomerModel $customerModel): Json
    {
        // echo "11221133";exit;
        $shopIds = $this->getCommonShopIdsCollectByUserType();
        $this->param['shopIds'] = $shopIds;
        $this->param['user_id'] = $this->userInfo['id'];
        $this->param['shop_person_id'] = $this->userInfo['shop_person_id'];

        $customerLossModel = new CustomerLossModel();
        // $data = $customerLossModel->getDataList($this->param);
        $data = $customerLossModel->getDataList1($this->param);


        if (!$data) {
            return resultArray(['error' => $customerModel->getError()]);
        }

        $is_export = $this->param['is_export'] ?? 0;

        if($is_export == 1){
            $is_join_shop = $this->param['is_join_shop'] ?? 0;

            if($is_join_shop == 8){
                $excelName = "保有顾客列表";
            }
            if($is_join_shop == 4){
                $excelName = "流失预警列表";
            }

            if($is_join_shop == 3){
                $excelName = "流失顾客列表";
            }


            $callName = [
                'create_time'   =>'录入时间',
                'customer_name' =>'姓名',
                'telephone' =>'手机号',
                'sex' =>'性别',
                'age' =>'年龄',
                'birthday'   =>'生日',
                'new_join_shop_time' =>'进店时间',
                'source_name' =>'来源',
                'grade_name' =>'会员等级',
                'nurse_name' =>'护理师',
                'shop_name'   =>'门店/门诊',
                'class_name' =>'客户分类',
                'final_consumption' =>'消费时间',
                'final_service' =>'服务时间',
                'last_kftime' =>'回访时间',
                'consumption'   =>'消费金额',
                'sum_money'   =>'总消费金额',
                'shop_name' =>'核销门店',
                'scar_aggregate' =>'疤痕类型',
            ];

            ////////////////////////////////////////////////周一导出使用////////////////////////////////////////

            // $arr = [];

            // $shopArr = [];
            // $k = 0;

            // foreach ($data['list'] as $key => $value) {
            //     $shopName = $value['shop_name'];
            //     $lName = $value['class_name'];

            //     if(in_array($shopName,$shopArr)){

            //     }else{
            //         array_push($shopArr,$shopName);
            //     }
            // }

            // $list = [];
            // foreach ($shopArr as $key => $value) {
            //     $one = [];

            //     $aL = 0;
            //     $bL = 0;
            //     $cL = 0;
            //     $dL = 0;

            //     foreach ($data['list'] as $key1 => $value1) {

            //         if($value == $value1['shop_name']){
            //             if($value1['class_name']=='A类客户'){
            //                 $aL++;
            //             }

            //             if($value1['class_name']=='B类客户'){
            //                 $bL++;
            //             }

            //             if($value1['class_name']=='C类客户'){
            //                 $cL++;
            //             }

            //             if($value1['class_name']=='D类客户'){
            //                 $dL++;
            //             }
            //         }

            //     }

            //     $one['shopName']=$value;
            //     $one['aL']=$aL;
            //     $one['bL']=$bL;
            //     $one['cL']=$cL;
            //     $one['dL']=$dL;

            //     array_push($list,$one);

            // }

            // $callName = [
            //     'shopName'   =>'门店名',
            //     'aL'         =>'A类客户',
            //     'bL'         =>'B类客户',
            //     'cL'         =>'C类客户',
            //     'dL'         =>'D类客户',

            // ];

            // if($is_join_shop == 4){
            //     $excelName = "流失预警统计";
            // }

            // if($is_join_shop == 3){
            //     $excelName = "流失顾客统计";
            // }

            // $downUrl = exportExcelData($excelName,$callName,$list,'xlsx',1);
            // return resultArray(['data' =>['url' => 'http://'.$_SERVER['HTTP_HOST'].$downUrl],'error' => '获取成功']);
            // exit;

            $downUrl = exportExcelData($excelName,$callName,$data['list'],'xlsx',1);
            return resultArray(['data' =>['url' => 'http://'.$_SERVER['HTTP_HOST'].$downUrl],'error' => '获取成功']);
            exit;

        }

        return resultArray(['data' => $data]);
    }
}
